#-*-coding:utf8;-*-

from ylTDX2 import * #from ylTDX import all def function
from  _utinity import *

"""
DRAWTEXT_FIX(1,0.00,0.00,0,'【趋势线注释：粉+红=满仓，粉+绿=六成，蓝+红=四成，蓝+绿=空仓】'),COLORRED;

VAR1:=IF(YEAR>2999 AND MONTH>5,0,1);
VAR2:=IF(HIGH=CLOSE,1.10*CLOSE,HIGH)*VAR1;
VAR3:=IF(HIGH=CLOSE,0.90*CLOSE,LOW)*VAR1;
短期值1:=EMA((HIGH+LOW)/2,5);
顶部线1:=EMA(EMA(VAR2,6)*1.05,8);
底部线1:=EMA(EMA(VAR3,6)*0.97,8);
VARA1:=EXPMA(CLOSE,5);
VARB1:=EXPMA(CLOSE,10);
VARC1:=EXPMA(CLOSE,20);
VARD1:=EXPMA(CLOSE,50);
VARE1:=(VARA1+VARB1+VARC1+VARD1)/4;
天线1:MA(VARE1,4);
天线2:=MA(VARE1,5)+MA(VARE1,5)*0.03;
天线3:MA(VARE1,4)-MA(VARE1,4)*0.03;
NOTEXT天线01:IF(天线1>REF(天线1,1),天线1,DRAWNULL),COLORRED;
NOTEXT天线11:IF(天线1<REF(天线1,1),天线1,DRAWNULL),COLORGREEN;
NOTEXT天线03:IF(天线3>REF(天线3,1),天线3,DRAWNULL),COLORRED;
NOTEXT天线13:IF(天线3<REF(天线3,1),天线3,DRAWNULL),COLORGREEN;
STICKLINE(天线1>REF(天线1,1),天线1,天线3,0,0),COLORRED;
STICKLINE(天线1<REF(天线1,1),天线1,天线3,0,0),COLORGREEN;

空:=REF(C,1)>REF(天线3,1) AND C<天线3;
多:=REF(C,1)<REF(天线1,1) AND C>天线1;
空1:=TFILTER(多,空,2);
多1:=TFILTER(多,空,1);

DRAWTEXT(空1,天线1*1.04,'空'),COLORGREEN;
DRAWTEXT(多1,天线3*0.96,'多'),COLORRED;
DRAWICON(空1,天线1*1.02,2);
DRAWICON(多1,天线3*0.98,1);

VAR11:=IF(YEAR>2999 AND MONTH>5,0,1);
VAR12:=IF(HIGH=CLOSE,1.10*CLOSE,HIGH)*VAR11;
VAR13:=IF(HIGH=CLOSE,0.90*CLOSE,LOW)*VAR11;
长期值2:=EMA((HIGH+LOW)/2,5);
顶部线2:=EMA(EMA(VAR12,6)*1.05,8);
底部线2:=EMA(EMA(VAR13,6)*0.97,8);
VAR18:=(HIGH+LOW+CLOSE)/3;
VARA2:=EMA(CLOSE,5);
VARB2:=EMA(CLOSE,10);
VARC2:=EMA(CLOSE,20);
VARD2:=EMA(CLOSE,50);
VARE2:=(VARA2+VARB2+VARC2+VARD2)/4;
天线12:=EMA(VARE2,60);
天线22:=EMA(VARE2,60)+EMA(VARE2,60)*0.03;
天线32:=EMA(VARE2,55)-EMA(VARE2,55)*0.03;
NOTEXT天线012:IF(天线12>REF(天线12,1),天线12,DRAWNULL),COLORMAGENTA;
NOTEXT天线112:IF(天线12<REF(天线12,1),天线12,DRAWNULL),COLORBLUE;
NOTEXT天线032:IF(天线32>REF(天线32,1),天线32,DRAWNULL),COLORMAGENTA;
NOTEXT天线132:IF(天线32<REF(天线32,1),天线32,DRAWNULL),COLORBLUE;
STICKLINE(天线12>REF(天线12,1),天线12,天线32,0,0),COLORMAGENTA;
STICKLINE(天线12<REF(天线12,1),天线12,天线32,0,0),COLORBLUE;
"""


def dkcw(code,file_name=None):                      
    df=get_data(code)
    OPEN=tdx(df["Open"].to_list()) #open 是file open
    HIGH=tdx(df["High"].to_list())
    LOW=tdx(df["Low"].to_list())
    CLOSE=tdx(df["Close"].to_list())
    VOL=tdx(df["Volume"].to_list())
    AMOUNT=tdx(df["Amount"].to_list())

    VAR2=IF(HIGH==CLOSE,CLOSE*1.1,HIGH)
    VAR3=IF(HIGH==CLOSE,CLOSE*0.9,LOW)
    短期值1=EMA((HIGH+LOW)/2,5)
    顶部线1=EMA(EMA(VAR2,6)*1.05,8)
    底部线1=EMA(EMA(VAR3,6)*0.97,8)
    VARA1=EMA(CLOSE,5)
    VARB1=EMA(CLOSE,10)
    VARC1=EMA(CLOSE,20)
    VARD1=EMA(CLOSE,50)
    VARE1=(VARA1+VARB1+VARC1+VARD1)/4
    天线1=MA(VARE1,4)   
    # 天线2=ADD(MA(VARE1,5),multiply(MA(VARE1,5),0.03))
    天线3=MA(VARE1,4)-MA(VARE1,4)*0.03# 天线3<天线1 ,等距 MA(VARE1,4)*0.03
    RED_GREEN=IF(天线1>REF(天线1,1),1,2)#1 RED 2 GREEN
    空=REF(CLOSE,1)>REF(天线3,1) & CLOSE<天线3
    多=REF(CLOSE,1)<REF(天线1,1) & CLOSE>天线1
    空1=TFILTER(多,空,2) #用法:TFILTER(买入条件,卖出条件,N);过滤掉买入(卖出)信号发出后,下一个反向信号发出前的所有买入(卖出)信号,
                        # N=1表示仅对买入信号过滤;
                        # N=2表示仅对卖出信号过滤;
                        # N=0表示对买入和卖出信号都过滤,返回1,2表示买入或卖出条件成立;
                        # 同一K线上只能有一个信号;
    多1=TFILTER(多,空,1)
    VAR11=1
    VAR12=IF(HIGH==CLOSE,CLOSE*1.1,HIGH)*VAR11
    VAR13=IF(HIGH==CLOSE,CLOSE*0.9,LOW)*VAR11
    长期值2=EMA((HIGH+LOW)/2,5)
    顶部线2=EMA(EMA(VAR12,6)*1.05,8)
    底部线2=EMA(EMA(VAR13,6)*0.97,8)
    VAR18=(HIGH+LOW+CLOSE)/3
    VARA2=EMA(CLOSE,5)
    VARB2=EMA(CLOSE,10)
    VARC2=EMA(CLOSE,20)
    VARD2=EMA(CLOSE,50)
    VARE2=(VARA2+VARB2+VARC2+VARD2)/4
    天线12=EMA(VARE2,60)
    MAGENTA_BLUE=IF(天线12>REF(天线12,1),3,4) #3 MAGENTA ,4 BLUE

    duokongcangwei=[]
    for i in range(len(RED_GREEN)):#粉+红=满仓，粉+绿=六成，蓝+红=四成，蓝+绿=空仓
        if(RED_GREEN[i]==1 and MAGENTA_BLUE[i]==3):
            duokongcangwei.append(1)
        elif(RED_GREEN[i]==2 and MAGENTA_BLUE[i]==3):
            duokongcangwei.append(0.6)
        elif(RED_GREEN[i]==1 and MAGENTA_BLUE[i]==4):
            duokongcangwei.append(0.4)
        else:
            duokongcangwei.append(0)

    if file_name is not None:
        output_File = open(file_name,'a+') 
        output_File.write('{},{},{}\n'.format(str(code).zfill(6),str(round(duokongcangwei[-1],2)),df['close'][-1]))
        output_File.flush()
        return "%s complete"%str(code).zfill(6)

if __name__=='__main__':
    # duokong(799)
    sort_stratedy(dkcw,ascending=False)
